package com.iakuil.app.api.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebApp总配置
 *
 * @author Kai
 * @date 2021/6/21 17:44
 **/
@Configuration
@EnableEncryptableProperties
public class ApplicationConfig implements WebMvcConfigurer {

    /**
     * Sa-Token路由拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handle -> {
                SaRouter
                    .match("/**")

                    .notMatch("/")
                    .notMatch("/**.js")
                    .notMatch("/**.css")
                    .notMatch("/**.html")

                    .notMatch("/**.svg")
                    .notMatch("/**.pdf")
                    .notMatch("/**.jpg")
                    .notMatch("/**.png")
                    .notMatch("/**.ico")

                    .notMatch("/webjars/**")
                    .notMatch("/swagger-resources/**")
                    .notMatch("/*/api-docs/**")
                    .notMatch("/doc.html")
                    .notMatch("/swagger-ui.html")

                    .notMatch("/api/v1/user/login", "/api/v1/user/register")
                    .check(r -> StpUtil.checkLogin());
            }))
            .addPathPatterns("/**");
    }

    /**
     * 忽略MyBatis扫描警告
     */
    @Mapper
    public interface NoWarnMapper {
    }
}
